//
//  UserModel.swift
//  GoldStar
//
//  Created by TRS-Mobile on 2025/6/24.
//

import UIKit
import SwiftyUserDefaults

class UserModel: Codable, DefaultsSerializable {
    var name: String = ""
    var createDate = Date()
    var uid = UUID()
    
    var flowers: [RedFlower] = []
    
    init(name: String,
         createDate: Date = Date(),
         uid: UUID = UUID())
    {
        self.name = name
        self.createDate = createDate
        self.uid = uid
    }
    
    var flowerCount: Int {
        flowers.count
    }
    
//    required init(from decoder: any Decoder) throws {
//        try super.init(from: decoder)
//    }
}

extension DefaultsKeys {
    var localUsers: DefaultsKey<[UserModel]> {
        .init("localUsers", defaultValue: [])
    }
    var launchCount: DefaultsKey<Int> {
        .init("launchCount", defaultValue: 0)
    }
}

class UserViewModel: DefaultTableViewCellViewModel {
    let user: UserModel
    init(user: UserModel) {
        self.user = user
        super.init()
        self.title.accept(user.name)
        self.date.accept(user.createDate)
        self.flowerCount.accept(user.flowerCount)
    }
}
